<template>
  <div class="stock-record-container">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="产品">
        <el-select v-model="queryParams.product" placeholder="请选择产品">
          <el-option v-for="item in products" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="操作类型">
        <el-select v-model="queryParams.type" placeholder="请选择类型">
          <el-option label="入库" value="1"></el-option>
          <el-option label="出库" value="2"></el-option>
          <el-option label="盘点" value="3"></el-option>
          <el-option label="调拨" value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间">
        <el-date-picker v-model="queryParams.start_time" type="datetime" placeholder="选择开始时间"></el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间">
        <el-date-picker v-model="queryParams.end_time" type="datetime" placeholder="选择结束时间"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getList">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="stockRecords" border style="width: 100%">
      <el-table-column prop="product.name" label="产品名称" width="180"></el-table-column>
      <el-table-column prop="product.code" label="产品编码" width="180"></el-table-column>
      <el-table-column prop="type" label="操作类型" width="100">
        <template #default="{ row }">
          {{ getRecordTypeLabel(row.type) }}
        </template>
      </el-table-column>
      <el-table-column prop="quantity" label="操作数量" width="100"></el-table-column>
      <el-table-column prop="before_stock" label="操作前库存" width="120"></el-table-column>
      <el-table-column prop="after_stock" label="操作后库存" width="120"></el-table-column>
      <el-table-column prop="operator.name" label="操作人" width="100"></el-table-column>
      <el-table-column prop="create_time" label="操作时间"></el-table-column>
    </el-table>

    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :current-page="queryParams.page"
      :page-size="queryParams.size"
      @current-change="handlePageChange"
    ></el-pagination>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import request from '@/utils/request'
import { ElMessage } from 'element-plus'

const stockRecords = ref([])
const total = ref(0)
const queryParams = ref({
  page: 1,
  size: 10,
  product: '',
  type: '',
  start_time: '',
  end_time: ''
})
const products = ref([])

// 获取产品列表
const getProducts = async () => {
  const res = await request.get('/api/product/')
  products.value = res.records
}

// 获取库存记录列表
const getList = async () => {
  const res = await request.get('/api/stock-record/', {
    params: queryParams.value
  })
  stockRecords.value = res.records
  total.value = res.total
}

// 分页变化
const handlePageChange = (newPage) => {
  queryParams.value.page = newPage
  getList()
}

// 操作类型标签
const getRecordTypeLabel = (type) => {
  switch (type) {
    case 1:
      return '入库'
    case 2:
      return '出库'
    case 3:
      return '盘点'
    case 4:
      return '调拨'
    default:
      return ''
  }
}

onMounted(() => {
  getProducts()
  getList()
})
</script>

<style scoped>
.stock-record-container {
  padding: 20px;
}
</style>
